#pragma once

#include <cmath>
#define PI 3.1415926
#define D 300
#define R 300
#define F 0.466
#define L 1/6*2*PI*R

double dydx(double f,double x){return x/(2*D*f);}
double func(double o,double f,double h){return (4*D*f*sin(o)+sqrt(16*D*D*f*f*sin(o)*sin(o)+16*D*f*(R+h)*cos(o)*cos(o)))/(2*cos(o)*cos(o));}
double z(double f ,double h,double x){return (x*x-4*D*f*(R+h))/(4*D*f);}
double middle(double f,double h){return D*f-R-h;}
double onleft(double f,double h,double x){return -(middle(f,h)-z(f,h,x))/300+middle(f,h);}
double onright(double f,double h,double x){return (middle(f,h)-z(f,h,x))/300+middle(f,h);}
double absolute(double x){
    if((x-0)<0){
        return -x;
    }
    return x;
}
double inf(double f ,double x){return 1/2*(x*sqrt(x*x+1)+log(absolute(sqrt(x*x+1))+x));}
void max(double x , double *max){
    if((x-*max)>0){
        *max=x;
    }
}
void min(double x , double *min){
    if((x-*min)<0){
        *min=x;
    }
}
double length(double f,double h,double x1,double x2){
    return inf(f,x2)-inf(f,x1);
}
double cost(double *f,double *h,double x1,double x2){
    double a = *f;
    double b = *h;
    double dertal,dertak;
    dertal = absolute(length(a,b,x1,x2)-L);
    dertak = absolute(dydx(a,x1)+sqrt(3)/3);
    return dertal+dertak;
}
bool ismatch(double f, double h){
    for(int i=0;i<(PI/3)/0.0001;i++){
        double o = -2*PI/3 +(double)i*0.0001;
        if(absolute(func(o,f,h)-R)>0.6){
            return false;
        }
    }
    return true;
}

// double safe(double *f,double *h,double x1,double x2){
//     double dertal,dertak;
//     dertal = absolute(length(*f,*h,x1,x2)-L);
//     dertak = absolute(dydx(*f,x1)+sqrt(3)/3);
//     return dertal+dertak;
// }